package top.drinyeit.weilong.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scu.miomin.shswiperefresh.core.SHSwipeRefreshLayout;
import com.scu.miomin.shswiperefresh.view.SHListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import top.drinyeit.weilong.R;
import top.drinyeit.weilong.adapter.AddressAdapter;
import top.drinyeit.weilong.base.BaseActivity;
import top.drinyeit.weilong.model.AddressBean;
import top.drinyeit.weilong.presenter.MyAddressPresenter;
import top.drinyeit.weilong.util.StatusBarUtil;

/**
 * Created by bigHead on 2017/11/13.
 */

public class MyAddressActivity extends BaseActivity {

    private TextView addNew;
    public SHListView addressListView;
    public SHSwipeRefreshLayout shSwipeRefreshLayout;

    public MyAddressPresenter myAddressPresenter;
    boolean clickable = false;
    public int pageIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_myaddress);

        addNew = findViewById(R.id.addNew);
        addressListView = findViewById(R.id.addressListView);
        shSwipeRefreshLayout = findViewById(R.id.mShswipeRefreshLayout);


        initIntent();
        initData();
        init();
        inList();
        setTitle(true, "收货地址");
    }

    private void initIntent() {
        if (getIntent().getExtras() != null) {
            clickable = getIntent().getExtras().getBoolean("clickable");
        }
    }

    private void initData() {
        myAddressPresenter = new MyAddressPresenter(this);
    }

    public void inList() {
        myAddressPresenter.getAdd(1);
    }

    private void initRefresh() {
        shSwipeRefreshLayout.setOnRefreshListener(new SHSwipeRefreshLayout.SHSOnRefreshListener() {
            @Override
            public void onRefresh() {
                myAddressPresenter.getAdd(1);
            }

            @Override
            public void onLoading() {
                myAddressPresenter.getAdd(pageIndex + 1);
            }

            @Override
            public void onRefreshPulStateChange(float v, int i) {

            }

            @Override
            public void onLoadmorePullStateChange(float v, int i) {

            }
        });
    }

    @Override
    public void init() {

        initRefresh();
        addNew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MyAddressActivity.this, AddNewAddressActivity.class);
                startActivityForResult(intent, 1);
            }
        });

        if (clickable) {
            addressListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = getIntent();
                    intent.putExtra("detail", myAddressPresenter.addressAdapter.getData().get(position).getAddress_detail_json());
                    intent.putExtra("consignee_name", myAddressPresenter.addressAdapter.getData().get(position).getConsignee_name());
                    intent.putExtra("id", myAddressPresenter.addressAdapter.getData().get(position).getId());
                    intent.putExtra("consignee_phone", myAddressPresenter.addressAdapter.getData().get(position).getConsignee_phone());
                    intent.putExtra("default_flag", myAddressPresenter.addressAdapter.getData().get(position).getDefault_flag());
                    setResult(1, intent);
                    finish();
                }
            });
        }
        LinearLayout im2;//返回按钮

        im2 = findViewById(R.id.ll_back);

        im2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (clickable) {
                    if (myAddressPresenter.addressAdapter == null) {
                        setResult(2);
                    } else {
                        if (myAddressPresenter.addressAdapter.getData().size() == 0) {
                            setResult(2);
                        }
                    }

                }
                finish();
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 1) {
            inList();
        }
    }

//    @Override
//    public void onActivityReenter(int resultCode, Intent data) {
//        super.onActivityReenter(resultCode, data);
//
//    }
}
